<!-- eslint-disable prettier/prettier -->
<template>
  <div class="user">
    <!-- 第2步： 父组件绑定子组件派发出来的事件，自己内部绑定一个新的函数 -->
    <page-search
     :searchFormConfig="searchFormConfig"
      @resetBtnClick="handleResetClick"
      @queryBtnClick="handleQueryClick"/>
    <div class="content">
      <!--第3步：  ref获取兄弟组件，并且在自己内部的函数中调用方法 -->
      <page-content
       :contentTableConfig="contentTableConfig"
       pageName="users"
       ref="pageContentRef"
       @createBtnClick="handleCreateData"
       @editBtnClick="handleEditData"
       ></page-content>
       <!-- 创建新用户的弹窗 -->
       <!-- C1. 把值传递给page-model 内部需要接收这个属性-->
        <page-model
        :defaultInfo="defaultInfo"
        ref="pageModelRef"
        pageName="users"
        :modelConfig="modelConfigRef"
        ></page-model>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, computed } from 'vue'

import pageSearch from '@/ components/page-search'
import { searchFormConfig } from '../user/config/search.config'

import pageContent from '@/ components/page-content'
import { contentTableConfig } from '../user/config/content.config'

import { usePageSearch } from '@/hooks/use-page-search'
import { usePageModel } from '@/hooks/use-page-model'

import pageModel from '@/ components/page-model/src/page-model.vue'
import { modelConfig } from './config/model.config'

import { useStore } from '@/store'

export default defineComponent({
  components: {
    pageSearch,
    pageContent,
    pageModel
  },
  setup() {
    //第4步： 使用usePageSearch hooks，内部定义了这三个函数，并且调用pageContent的方法。重新发送网络请求，更新数据
    const [pageContentRef, handleBtnClick, handleQueryClick] = usePageSearch()

    // key1: pageModel相关的hook逻辑--处理密码的逻辑
    const createCallback = () => {
      const passwordItem = modelConfig.formItems.find(
        (item) => item.field === 'password'
      )
      debugger
      passwordItem!.isHidden = false
      console.log('xinjian', passwordItem)
    }
    const editCallback = () => {
      const passwordItem = modelConfig.formItems.find(
        (item) => item.field === 'password'
      )
      debugger
      passwordItem!.isHidden = true
      console.log('bainxji', passwordItem)
    }
    //key2: 动态的添加部门和角色列表
    const store = useStore()
    //key3: 使用computed来监听vuex中数据的变化，重新执行computed内部的getter函数，重新刷新page-model组件
    const modelConfigRef = computed(() => {
      const departmentItem = modelConfig.formItems.find(
        (item) => item.field === 'departmentId'
      )
      departmentItem!.options = store.state.entireDepartment.map((item) => {
        return { title: item.name, value: item.id }
      })
      const roleItem = modelConfig.formItems.find(
        (item) => item.field === 'roleId'
      )
      roleItem!.options = store.state.entireRole.map((item) => {
        return { title: item.name, value: item.id }
      })
      return modelConfig
    })

    //key3: 继续优化usePageModel hooks，点击新建的时候执行调用回调函数 ，点击edit，执行edit回调函数。
    const [pageModelRef, defaultInfo, handleCreateData, handleEditData] =
      usePageModel(createCallback, editCallback)
    return {
      searchFormConfig,
      contentTableConfig,
      modelConfigRef,
      handleBtnClick,
      handleQueryClick,
      pageContentRef,
      handleCreateData,
      handleEditData,
      pageModelRef,
      defaultInfo
    }
  }
})
</script>

<style scoped lang="less"></style>
